package com.gxa.service.Impl;

import com.gxa.common.R;
import com.gxa.mapper.zhy.AdministratorListMapper;
import com.gxa.pojo.Role;
import com.gxa.pojo.Users;
import com.gxa.pojo.zhy.AdminInformation;
import com.gxa.pojo.zhy.JsonResult;
import com.gxa.pojo.zhy.Paging;
import com.gxa.service.AdministratorListService;
import com.gxa.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/06 16:59
 * Description:
 * Version: V1.0
 */
@Service
public class AdministratorListServiceImpl implements AdministratorListService {
    @Autowired
    private AdministratorListMapper administratorListMapper;
    /**
     * 前端返回对应的状态值和管理员id，修改管理员的状态
     */

    @Override
    public R updateUserStatue(Integer user_id, Integer statue) {
        R r=new R();
        int resulrUpdate=administratorListMapper.updateUserStatue(user_id,statue);
        if(resulrUpdate>0){
            r.setCode(200);
            r.setMsg("修改状态成功");
        }else{
            r.setCode(500);
            r.setMsg("后台服务错误，请联系管理员");
        }
        return r;
    }
    /**
     * 查询获取所有的角色，用于下拉框显示
     * @param
     * @param
     * @return
     */
    @Override
    public R queryAllRoles() {
        R r=new R();
        List<Role> roleList=administratorListMapper.queryAllRoles();
        if(roleList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(roleList);
        }else if(roleList==null){
            r.setCode(504);
            r.setMsg("查询失败");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }
        return r;
    }
    /**
     * 根据管理员的id（user_id）删除管理员
     * @param user_id
     * @return
     */
    @Override
    public R delUserbyuser_id(Integer user_id) {
        R r=new R();
        int result=administratorListMapper.delUserbyuser_id(user_id);
        if(result>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }
        return r;
    }
    /**
     * 添加管理员
     * @param username
     * @param user_password
     * @param emp_number
     * @param role_id
     * @return
     */
    @Override
    public R addUsers(String username, String user_passwords, String emp_number, Integer role_id) {
        R r=new R();
        String user_password= MD5Utils.encrypt(user_passwords,MD5Utils.MD5_KEY);
        //判断该账号是否已经存在
        int result1=administratorListMapper.queryUserByusername(username);
        if(result1>0){
            r.setCode(504);
            r.setMsg("该账号已经存在，不可重复注册");
        }else {
            //判断该员工编号是否已经是员工
            int result2=administratorListMapper.queryEmpByemp_number(emp_number);
            if(result2>0){

                //判断该员工编号是否已经注册
                int result4=administratorListMapper.judgeUsersIsResisgterByemp_number(emp_number);
                if(result4>0){
                    r.setCode(506);
                    r.setMsg("该员工编号已经注册");
                }else{
                    //添加管理员
                    int result3= administratorListMapper.addUsers(username,user_password,emp_number,role_id);
                    if(result3>0){
                        r.setCode(200);
                        r.setMsg("添加管理员成功");
                    }else {
                        r.setCode(500);
                        r.setMsg("后台服务错误");
                    }
                }

            }else {
                r.setCode(503);
                r.setMsg("该员工编号不存在，要先成为员工才能成为管理员");
            }
        }
        return r;
    }
    /**
     * 返回所有的用户（管理员）信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryAllUsers(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult=new JsonResult();
        List<Object> resultList=new ArrayList<>();

        R r=new R();
        Integer totalSize=administratorListMapper.querytotalSize();
        Integer param1=(currentPage-1)*pageSize;
        Integer param2=pageSize;
        List<Object> adminInformationList=administratorListMapper.queryAllUsers(param1,param2);
        if(adminInformationList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalSize);
            resultList.add(jsonResult);
            resultList.add(adminInformationList);
            r.setData(resultList);
        }else if(adminInformationList==null) {
            r.setCode(504);
            r.setMsg("无数据");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }

        return r;
    }

    @Override
    public R updateUsersInformation(AdminInformation adminInformation) {
        R r=new R();
        //更改管理员的密码和角色

//        进行加密
        String oldUser_password= MD5Utils.encrypt(adminInformation.getOldUser_password(),MD5Utils.MD5_KEY);
//        adminInformation.setOldUser_password(oldUser_password);
//       更改管理员信息时， 判断输入的原密码是否正确
//        获取该用户原来已经加密后的密码
//        Integer queryResult=administratorListMapper.queryAdminInformationByinformationPassword(adminInformation);
        Users users=administratorListMapper.queryAdminInformationByinformationOldPassword(adminInformation);
        if(users.getUser_password().equals(oldUser_password)){
//            将新密码进行加密
            String User_password= MD5Utils.encrypt(adminInformation.getUser_password(),MD5Utils.MD5_KEY);
            adminInformation.setUser_password(User_password);
            int result1=administratorListMapper.updateUser(adminInformation);
            //更改性别、手机、邮箱
            int result2=administratorListMapper.updateEmp(adminInformation);
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(504);
            r.setMsg("原密码输入错误，修改失败");
        }



        return r;
    }

    /**
     * 关键字模糊搜索（管理员）信息的接口
     * @param paging
     * @return
     */
    @Override
//    public R queryAdminInformationByinformation(Paging paging) {
    public R queryAdminInformationByinformation(Integer currentPage,Integer pageSize,String message) {
        JsonResult jsonResult=new JsonResult();
        List<Object> resultList=new ArrayList<>();
        R r=new R();
//        Integer totalSize=administratorListMapper.queryAdminInformationByinformationtotalSize(paging.getMessage());
        Integer totalSize=administratorListMapper.queryAdminInformationByinformationtotalSize(message);
//        Integer param1=(paging.getCurrentPage()-1)*paging.getPageSize();
        Integer param1=(currentPage-1)*pageSize;
//        Integer param2=paging.getPageSize();
        Integer param2=pageSize;
//        List<AdminInformation> adminInformationList=administratorListMapper.queryAdminInformationByinformation(param1,param2,paging.getMessage());
        List<AdminInformation> adminInformationList=administratorListMapper.queryAdminInformationByinformation(param1,param2,message);
//        System.out.println(adminInformationList);
//        System.out.println(adminInformationList.isEmpty());
        if(!adminInformationList.isEmpty()){
            System.out.println("ok");
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalSize);
            resultList.add(adminInformationList);
            resultList.add(jsonResult);
            r.setData(resultList);
        }else if(adminInformationList.isEmpty()) {
            r.setCode(504);
            r.setMsg("无数据");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }

        return r;
    }
}
